package com.example.base.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.base.config.PageData;
import com.example.base.excetion.GlobalExcetion;
import com.example.base.limit.IPRateLimit;
import com.example.base.system.domain.SysLoginRecord;
import com.example.base.system.service.SysLoginRecordService;
import com.example.base.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 登录记录 前端控制器
 * </p>
 *
 * @author xiong
 * @since 2024-02-07
 */

@RestController
@Api(tags = {"登录记录的服务类"})
@RequestMapping("/sys-login-record")
public class SysLoginRecordController {

        @Autowired
        private SysLoginRecordService sysLoginRecordService;


        @DeleteMapping("/{id}")
        @IPRateLimit(name = "SysLoginRecord-del",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "登录记录的删除接口",notes = "登录记录的删除接口")
        public JSONObject delete(@PathVariable("id") Serializable id) throws GlobalExcetion{
                boolean flag = sysLoginRecordService.removeById(id);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @PostMapping("/del/batch")
        @IPRateLimit(name = "SysLoginRecord-delbatch",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "登录记录的批量删除接口",notes = "登录记录的批量删除接口")
        public JSONObject deleteBatch(@RequestBody List<Integer> ids) throws GlobalExcetion {
                boolean flag =  sysLoginRecordService.removeByIds(ids);
                if(flag){
                        return Result.successInfo("删除成功");
                }else {
                        return Result.errorInfo("删除失败");
                }
        }

        @GetMapping("/getall")
        @IPRateLimit(name = "SysLoginRecord-getall",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "登录记录的获取全部接口",notes = "登录记录的获取全部接口")
        public JSONObject findAll() throws GlobalExcetion {
                List<SysLoginRecord> list= sysLoginRecordService.list();
                return Result.successInfo(list);
        }

        @GetMapping("/{id}")
        @IPRateLimit(name = "SysLoginRecord-getid",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "登录记录的获取一个接口",notes = "登录记录的获取一个接口")
        public JSONObject findOne(@PathVariable("id") Serializable id) throws GlobalExcetion {
                SysLoginRecord data= sysLoginRecordService.getById(id);
                return Result.successInfo(data);
        }

        @GetMapping("/page")
        @IPRateLimit(name = "SysLoginRecord-page",msg = "系统检测到ip异常访问行为，已暂停访问")
        @ApiOperation(value = "登录记录的分页接口",notes = "登录记录的分页接口")
        public JSONObject findPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize) throws GlobalExcetion {
                QueryWrapper<SysLoginRecord> queryWrapper = new QueryWrapper<>();
                IPage<SysLoginRecord> data=sysLoginRecordService.page(new Page<>(page, pageSize),queryWrapper);
                PageData pageData=new PageData(data.getTotal(),page,pageSize,data.getRecords());
                return Result.successInfo(pageData);
        }
}

